/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canteen.sys.view;

import canteen.sys.model.entity.DepartmentMaster;
import canteen.sys.model.entity.DesignationMaster;
import canteen.sys.model.entity.EmployeeMaster;
import canteen.sys.model.entity.LoginMaster;
import canteen.sys.model.entity.SubDepartmentMaster;
import canteen.sys.model.generic.HibernateDAOFactory;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import org.hibernate.HibernateException;

/**
 *
 * @author TNT
 */
public class EmployeeEntry extends javax.swing.JInternalFrame implements ActionView<EmployeeMaster> {

    HibernateDAOFactory hibernateDAOFactory = HibernateDAOFactory.getInstance();
    DefaultTableModel modelTable;
    List<EmployeeMaster> listStore = null;

    /**
     * Creates new form EmployeeEntry
     */
    public EmployeeEntry() {
        initComponents();
//        Calendar calendar = Calendar.getInstance();
//        int index = cbDateOfJoin.getSelectedIndex();
        modelTable = (DefaultTableModel) tableEmployee.getModel();
        listStore = hibernateDAOFactory.getEmployeeMasterDAO().findAll();
        showList(listStore);
        getListDepartment();
        getListDesignation();
        ComboboxRecInfo<DepartmentMaster> combobox = (ComboboxRecInfo<DepartmentMaster>) cbDepartment.getSelectedItem();
        if (combobox != null) {
            int departmentId = combobox.getKeyData().getDeptId();
            getListSubDepartment(departmentId);
        }
        listenerTableModel();
        listSelectionListener();



    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        genderGroup = new javax.swing.ButtonGroup();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btsave = new javax.swing.JButton();
        btdelete = new javax.swing.JButton();
        btcancel = new javax.swing.JButton();
        txtEmpID = new javax.swing.JTextField();
        txtLastName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        rbStatus = new javax.swing.JRadioButton();
        jLabel4 = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        txtFistName = new javax.swing.JTextField();
        txtMidName = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        txtAddress1 = new javax.swing.JTextField();
        txtAddress2 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        rbMale = new javax.swing.JRadioButton();
        rbFemale = new javax.swing.JRadioButton();
        jLabel10 = new javax.swing.JLabel();
        txtMobilePhone = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtHomePhone = new javax.swing.JTextField();
        cbDepartment = new javax.swing.JComboBox();
        cbDesignation = new javax.swing.JComboBox();
        cbSubDepartment = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        cbDateOfJoin = new org.freixas.jcalendar.JCalendarCombo();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        messageLabel = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        cbRole = new javax.swing.JComboBox();
        jLayeredPane2 = new javax.swing.JLayeredPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableEmployee = new javax.swing.JTable();

        setClosable(true);
        setTitle("Employee Entry");

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Employee Details"));
        jLayeredPane1.setToolTipText("");

        jLabel1.setText("ID");
        jLabel1.setBounds(10, 20, 30, 20);
        jLayeredPane1.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Name");
        jLabel2.setBounds(10, 70, 40, 20);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btsave.setText("Save");
        btsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btsaveActionPerformed(evt);
            }
        });
        btsave.setBounds(10, 350, 80, 23);
        jLayeredPane1.add(btsave, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btdelete.setText("Delete");
        btdelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btdeleteActionPerformed(evt);
            }
        });
        btdelete.setBounds(110, 350, 80, 23);
        jLayeredPane1.add(btdelete, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btcancel.setText("Cancel");
        btcancel.setEnabled(false);
        btcancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btcancelActionPerformed(evt);
            }
        });
        btcancel.setBounds(210, 350, 80, 23);
        jLayeredPane1.add(btcancel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtEmpID.setEnabled(false);
        txtEmpID.setBounds(100, 20, 80, 20);
        jLayeredPane1.add(txtEmpID, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtLastName.setBounds(300, 70, 100, 20);
        jLayeredPane1.add(txtLastName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Address 2");
        jLabel3.setBounds(10, 130, 80, 20);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        rbStatus.setSelected(true);
        rbStatus.setBounds(100, 290, 21, 21);
        jLayeredPane1.add(rbStatus, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setText("Code");
        jLabel4.setBounds(255, 20, 30, 20);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtCode.setBounds(300, 20, 100, 20);
        jLayeredPane1.add(txtCode, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtFistName.setBounds(100, 70, 100, 20);
        jLayeredPane1.add(txtFistName, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtMidName.setBounds(200, 70, 100, 20);
        jLayeredPane1.add(txtMidName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("First Name(*)");
        jLabel5.setBounds(100, 50, 100, 14);
        jLayeredPane1.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText("Middle Name");
        jLabel6.setBounds(200, 50, 100, 14);
        jLayeredPane1.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setText("Last Name(*)");
        jLabel7.setBounds(300, 50, 100, 14);
        jLayeredPane1.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setText("Address 1");
        jLabel8.setBounds(10, 100, 80, 20);
        jLayeredPane1.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtAddress1.setBounds(100, 100, 300, 20);
        jLayeredPane1.add(txtAddress1, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtAddress2.setBounds(100, 130, 300, 20);
        jLayeredPane1.add(txtAddress2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel9.setText("Gender");
        jLabel9.setBounds(10, 160, 50, 14);
        jLayeredPane1.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genderGroup.add(rbMale);
        rbMale.setSelected(true);
        rbMale.setText("Male");
        rbMale.setBounds(100, 160, 60, 23);
        jLayeredPane1.add(rbMale, javax.swing.JLayeredPane.DEFAULT_LAYER);

        genderGroup.add(rbFemale);
        rbFemale.setText("Female");
        rbFemale.setBounds(200, 160, 80, 23);
        jLayeredPane1.add(rbFemale, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel10.setText("Date of Join");
        jLabel10.setBounds(210, 260, 80, 20);
        jLayeredPane1.add(jLabel10, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtMobilePhone.setBounds(300, 200, 100, 20);
        jLayeredPane1.add(txtMobilePhone, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel11.setText("Status");
        jLabel11.setBounds(10, 290, 80, 20);
        jLayeredPane1.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtHomePhone.setBounds(100, 200, 100, 20);
        jLayeredPane1.add(txtHomePhone, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbDepartment.setBounds(100, 230, 100, 20);
        jLayeredPane1.add(cbDepartment, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbDesignation.setBounds(100, 260, 100, 20);
        jLayeredPane1.add(cbDesignation, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbSubDepartment.setBounds(300, 230, 100, 20);
        jLayeredPane1.add(cbSubDepartment, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel12.setText("Home Phone");
        jLabel12.setBounds(10, 200, 80, 20);
        jLayeredPane1.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel13.setText("Department");
        jLabel13.setBounds(10, 230, 80, 20);
        jLayeredPane1.add(jLabel13, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel14.setText("Mobile Phone");
        jLabel14.setBounds(210, 200, 80, 20);
        jLayeredPane1.add(jLabel14, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel15.setText("Sub Department");
        jLabel15.setBounds(210, 230, 80, 20);
        jLayeredPane1.add(jLabel15, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbDateOfJoin.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MM/dd/yyyy" }));
        cbDateOfJoin.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
        cbDateOfJoin.setBounds(300, 260, 100, 20);
        jLayeredPane1.add(cbDateOfJoin, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel16.setText("Designation");
        jLabel16.setBounds(10, 260, 80, 20);
        jLayeredPane1.add(jLabel16, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel17.setText("Message:");
        jLabel17.setBounds(10, 320, 70, 14);
        jLayeredPane1.add(jLabel17, javax.swing.JLayeredPane.DEFAULT_LAYER);

        messageLabel.setForeground(new java.awt.Color(255, 0, 51));
        messageLabel.setText(" ");
        messageLabel.setBounds(100, 314, 300, 20);
        jLayeredPane1.add(messageLabel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel18.setText("Role");
        jLabel18.setBounds(210, 290, 70, 20);
        jLayeredPane1.add(jLabel18, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbRole.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "employee", "admin" }));
        cbRole.setBounds(300, 290, 100, 20);
        jLayeredPane1.add(cbRole, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLayeredPane2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder(""), "Click on Record to Delete or Edit"));

        tableEmployee.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "ID", "Name", "Department", "Designation", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableEmployee);
        tableEmployee.getColumnModel().getColumn(0).setMaxWidth(50);
        tableEmployee.getColumnModel().getColumn(4).setMaxWidth(100);

        jScrollPane1.setBounds(10, 20, 520, 380);
        jLayeredPane2.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(jLayeredPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 407, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 539, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jLayeredPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
            .add(jLayeredPane1)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btsaveActionPerformed
        // TODO add your handling code here:
        try {
            EmployeeMaster employeeMaster = new EmployeeMaster();
            if (btsave.getText().equals("Update")) {
                btsave.setText("Save");
                btcancel.setEnabled(false);
                validInsertOrUpdateEmployee(employeeMaster);
                updateRow(employeeMaster, tableEmployee.getSelectedRow());
            } else {
                validInsertOrUpdateEmployee(employeeMaster);
                addRow(employeeMaster, true);
            }

        } catch (HibernateException ex) {
            JOptionPane.showMessageDialog(this, "Add new Employee Fail!\n Error: " + ex.getMessage(),
                    "Error", JOptionPane.ERROR_MESSAGE);
        } catch (NumberFormatException ex) {
            messageLabel.setText("Error: " + ex.getMessage());
        }
    }//GEN-LAST:event_btsaveActionPerformed

    private void btdeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btdeleteActionPerformed
        // TODO add your handling code here:
        int indexRow = tableEmployee.getSelectedRow();
        if (indexRow != -1) {
            try {
                if (modelTable.getRowCount() > 1) {
                    hibernateDAOFactory.getEmployeeMasterDAO().makeTransient(getObject(indexRow));
                    deleteRow(indexRow);
                }
            } catch (HibernateException ex) {
                JOptionPane.showMessageDialog(this, "Can't delete Department\nError: " + ex.getMessage());
            } catch (Exception ex) {
                messageLabel.setText("Error: " + ex.getMessage());
            }
        } else {
            JOptionPane.showMessageDialog(this, "No row selected!", "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btdeleteActionPerformed

    private void btcancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btcancelActionPerformed
        // TODO add your handling code here:
        //        DepartmentMaster departmentMaster = new DepartmentMaster();
        //        departmentMaster.setDepartmentName("TNTuyen");
        //        departmentMaster.setStatus(false);
        //        updateRow(departmentMaster, 2);
//        this.dispose();
        txtEmpID.setText("");
        btsave.setText("Save");
        btcancel.setEnabled(false);
    }//GEN-LAST:event_btcancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btcancel;
    private javax.swing.JButton btdelete;
    private javax.swing.JButton btsave;
    private org.freixas.jcalendar.JCalendarCombo cbDateOfJoin;
    private javax.swing.JComboBox cbDepartment;
    private javax.swing.JComboBox cbDesignation;
    private javax.swing.JComboBox cbRole;
    private javax.swing.JComboBox cbSubDepartment;
    private javax.swing.ButtonGroup genderGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JLayeredPane jLayeredPane2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JRadioButton rbFemale;
    private javax.swing.JRadioButton rbMale;
    private javax.swing.JRadioButton rbStatus;
    private javax.swing.JTable tableEmployee;
    private javax.swing.JTextField txtAddress1;
    private javax.swing.JTextField txtAddress2;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtEmpID;
    private javax.swing.JTextField txtFistName;
    private javax.swing.JTextField txtHomePhone;
    private javax.swing.JTextField txtLastName;
    private javax.swing.JTextField txtMidName;
    private javax.swing.JTextField txtMobilePhone;
    // End of variables declaration//GEN-END:variables

    @Override
    public void deleteRow(int index) {
        if (modelTable.getRowCount() > 0) {
            if (index > 0) {
                tableEmployee.setRowSelectionInterval(index - 1, index - 1);
                modelTable.removeRow(index);
            } else {
                tableEmployee.setRowSelectionInterval(index + 1, index + 1);
                modelTable.removeRow(0);
            }

            listStore.remove(listStore.size() - 1 - index);
        } else {
            JOptionPane.showMessageDialog(this, "There is no row to delete!", "Warning!", JOptionPane.WARNING_MESSAGE);
        }
    }

    @Override
    public void updateRow(EmployeeMaster row, int index) {
        modelTable.setValueAt(row.geteFName() + " " + row.geteMName() + " " + row.geteLName(), index, 1);
        modelTable.setValueAt(row.getDepartment().getDepartmentName(), index, 2);
        modelTable.setValueAt(row.getDesignational().getDesignationName(), index, 3);
        if (row.isStatus()) {
            modelTable.setValueAt("enable", index, 4);
        } else {
            modelTable.setValueAt("disable", index, 4);
        }
    }

    @Override
    public void addRow(EmployeeMaster row, boolean posion) {
        String status = "disable";
        if (row.isStatus()) {
            status = "enable";
        }
        if (posion) {
            modelTable.insertRow(0, new Object[]{row.geteId() + "",
                        row.geteFName() + " " + row.geteMName() + " " + row.geteLName(),
                        row.getDepartment().getDepartmentName(),
                        row.getDesignational().getDesignationName(), status});
        } else {
            modelTable.insertRow(tableEmployee.getRowCount(), new Object[]{row.geteId() + "",
                        row.geteFName() + " " + row.geteMName() + " " + row.geteLName(),
                        row.getDepartment().getDepartmentName(),
                        row.getDesignational().getDesignationName(), status});
        }
        listStore.add(row);

    }

    @Override
    public final void showList(List<EmployeeMaster> list) {
        String status;
        for (int i = 0; i < 4; i++) {
            modelTable.removeRow(0);
        }
        for (EmployeeMaster item : list) {
            if (item.isStatus()) {
                status = "enable";
            } else {
                status = "disable";
            }
            modelTable.insertRow(0, new Object[]{item.geteId() + "",
                        item.geteFName() + " " + item.geteMName() + " " + item.geteLName(),
                        item.getDepartment().getDepartmentName(),
                        item.getDesignational().getDesignationName(), status});
        }
    }

    @Override
    public void showDetail(EmployeeMaster object) {
        txtEmpID.setText(object.geteId() + "");
        txtCode.setText(object.geteCode());
        txtFistName.setText(object.geteFName());
        txtMidName.setText((object.geteMName()));
        txtLastName.setText(object.geteLName());
        txtAddress1.setText(object.geteAddress1());
        txtAddress2.setText(object.geteAddress2());
        if (object.isEGender()) {
            rbMale.setSelected(true);
        } else {
            rbFemale.setSelected(true);
        }
        txtHomePhone.setText(object.getePhoneHome());
        txtMobilePhone.setText(object.geteMobile());
        cbDesignation.setSelectedItem(object.getDesignational().getDesignationName());
        cbDepartment.setSelectedItem(object.getDepartment().getDepartmentName());
        cbSubDepartment.setSelectedItem(object.getSubDepartment().getSubDepartmentName());
        cbDateOfJoin.setDate(object.geteDoj());
        rbStatus.setSelected(object.isStatus());
    }

    @Override
    public EmployeeMaster getObject(int index) {
        int employeeId = Integer.parseInt(modelTable.getValueAt(index, 0).toString());
        for (EmployeeMaster item : listStore) {
            if (item.geteId() == employeeId) {
                return item;
            }
        }
        return null;
    }

    @Override
    public boolean validObject(EmployeeMaster object) {
        if (object.geteCode().equals("") || object.geteCode() == null) {
            messageLabel.setText("Code must input!");
            return false;
        } else if (object.geteFName().equals("") || object.geteFName() == null) {
            messageLabel.setText("First Name must input!");
            return false;
        } else if (object.geteLName().equals("") || object.geteLName() == null) {
            messageLabel.setText("Last Name must input!");
            return false;
        } else if (object.geteAddress1().equals("") || object.geteAddress1() == null) {
            messageLabel.setText("Address1 must input!");
            return false;
        } else if (object.geteMobile().equals("") || object.geteMobile() == null) {
            messageLabel.setText("Mobile must input!");
            return false;
        } else if (object.getDepartment() == null || object.getSubDepartment() == null
                || object.getDesignational() == null) {
            return false;
        }
        messageLabel.setText(" ");
        return true;
    }

    public final void getListDepartment() {
        List<DepartmentMaster> listDepartment = hibernateDAOFactory.getDepartmentMasterDAO().findAllByStatus(true);
        if (listDepartment.size() > 0) {

            for (DepartmentMaster departmentMaster : listDepartment) {
                ComboboxRecInfo<DepartmentMaster> combobox = new ComboboxRecInfo<>();
                combobox.setName(departmentMaster.getDepartmentName());
                combobox.setKeyData(departmentMaster);
                cbDepartment.addItem(combobox);
            }
        }
    }

    public final void getListSubDepartment(int departmentId) {
        List<SubDepartmentMaster> listSubDepartment = hibernateDAOFactory.getSubDepartmentMasterDAO()
                .findAllByStatusAndDepartmentId(departmentId, true);
        if (listSubDepartment.size() > 0) {

            for (SubDepartmentMaster item : listSubDepartment) {
                ComboboxRecInfo<SubDepartmentMaster> combobox = new ComboboxRecInfo<>();
                combobox.setName(item.getSubDepartmentName());
                combobox.setKeyData(item);
                cbSubDepartment.addItem(combobox);
            }
        }
    }

    public final void getListDesignation() {
        List<DesignationMaster> listDesignation = hibernateDAOFactory.getDesignationMasterDAO().findAllByStatus(true);
        if (listDesignation.size() > 0) {
            for (DesignationMaster item : listDesignation) {
                ComboboxRecInfo<DesignationMaster> combobox = new ComboboxRecInfo<>();
                combobox.setName(item.getDesignationName());
                combobox.setKeyData(item);
                cbDesignation.addItem(combobox);
            }
        }
    }

    public final void listenerTableModel() {
        modelTable.addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getType() == TableModelEvent.DELETE) {
                    JOptionPane.showMessageDialog(null, "Delete Success!");
                }
                if (e.getType() == TableModelEvent.INSERT) {
                    JOptionPane.showMessageDialog(null, "Add new Employee Success!");
                } else if (e.getType() == TableModelEvent.UPDATE && e.getColumn() == 4) {
                    JOptionPane.showMessageDialog(null, "Update Employee Success!");
                }
            }
        });
    }

    public final void listSelectionListener() {
        tableEmployee.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int rowSelected = tableEmployee.getSelectedRow();
                showDetail(getObject(rowSelected));
                btsave.setText("Update");
                btcancel.setEnabled(true);
            }
        });
    }

    @Override
    public void setObject(EmployeeMaster employeeMaster) {
        if (!txtEmpID.getText().equals("") && txtEmpID.getText() != null) {
            employeeMaster.seteId(Integer.parseInt(txtEmpID.getText()));
        }
        employeeMaster.seteCode(txtCode.getText());
        employeeMaster.seteFName(txtFistName.getText());
        employeeMaster.seteMName(txtMidName.getText());
        employeeMaster.seteLName(txtLastName.getText());
        employeeMaster.seteAddress1(txtAddress1.getText());
        employeeMaster.seteAddress2(txtAddress2.getText());
        if (rbMale.isSelected()) {
            employeeMaster.seteGender(true);
        } else {
            employeeMaster.seteGender(false);
        }
        employeeMaster.setePhoneHome(txtHomePhone.getText());
        employeeMaster.seteMobile(txtMobilePhone.getText());
        ComboboxRecInfo<DepartmentMaster> comboboxDepartment = null;
        if (cbDepartment.getItemCount() > 0) {
            comboboxDepartment =
                    (ComboboxRecInfo<DepartmentMaster>) cbDepartment.getSelectedItem();
        } else {
            messageLabel.setText("No Department to Add Employee!");
            return;
        }
        ComboboxRecInfo<SubDepartmentMaster> comboboxSubDepartment = null;
        if (cbSubDepartment.getItemCount() > 0) {
            comboboxSubDepartment =
                    (ComboboxRecInfo<SubDepartmentMaster>) cbSubDepartment.getSelectedItem();
        } else {
            messageLabel.setText("No Sub Department to Add Employee!");
            return;
        }
        ComboboxRecInfo<DesignationMaster> comboboxDesignation = null;
        if (cbDesignation.getItemCount() > 0) {
            comboboxDesignation =
                    (ComboboxRecInfo<DesignationMaster>) cbDesignation.getSelectedItem();
        } else {
            messageLabel.setText("No Designation to Add Employee!");
            return;
        }
        employeeMaster.setDepartment(comboboxDepartment.getKeyData());
        employeeMaster.setSubDepartment(comboboxSubDepartment.getKeyData());
        employeeMaster.setDesignational(comboboxDesignation.getKeyData());
        employeeMaster.seteDoj(new Date(cbDateOfJoin.getCalendar().getTimeInMillis()));
        employeeMaster.setStatus(rbStatus.isSelected());
        LoginMaster lm = new LoginMaster();
        lm.setlName(txtFistName.getText() + "-" + txtCode.getText());
        lm.setlPassword("1234");
        lm.setlType(cbRole.getSelectedItem().toString());
        lm.setStatus(true);
        LoginMaster lmResult = hibernateDAOFactory.getLoginDAO().makePersistent(lm);
        if (lmResult != null) {
            employeeMaster.setLoginMaster(lmResult);
        } else {
            JOptionPane.showMessageDialog(this, "User is exist");
        }
    }

    private void validInsertOrUpdateEmployee(EmployeeMaster employeeMaster) {
        setObject(employeeMaster);
        if (validObject(employeeMaster)) {
            EmployeeMaster employeeResult = hibernateDAOFactory.getEmployeeMasterDAO().makePersistent(employeeMaster);
            if (employeeMaster.geteId() == 0) {
                listStore.add(employeeResult);
            } else {
                for (EmployeeMaster employeeMaster1 : listStore) {
                    if (employeeMaster1.geteId() == employeeResult.geteId()) {
                        int indexListStore = listStore.indexOf(employeeMaster1);
                        listStore.get(indexListStore).setDepartment(employeeResult.getDepartment());
                        listStore.get(indexListStore).setDesignational(employeeResult.getDesignational());
                        listStore.get(indexListStore).setLoginMaster(employeeResult.getLoginMaster());
                        listStore.get(indexListStore).setStatus(employeeResult.isStatus());
                        listStore.get(indexListStore).setSubDepartment(employeeResult.getSubDepartment());
                        listStore.get(indexListStore).seteAddress1(employeeResult.geteAddress1());
                        listStore.get(indexListStore).seteAddress2(employeeResult.geteAddress2());
                        listStore.get(indexListStore).seteCode(employeeResult.geteCode());
                        listStore.get(indexListStore).seteDoj(employeeResult.geteDoj());
                        listStore.get(indexListStore).seteFName(employeeResult.geteFName());
                        listStore.get(indexListStore).seteGender(employeeResult.isEGender());
                        listStore.get(indexListStore).seteId(employeeResult.geteId());
                        listStore.get(indexListStore).seteLName(employeeResult.geteLName());
                        listStore.get(indexListStore).seteMName(employeeResult.geteMName());
                        listStore.get(indexListStore).seteMobile(employeeResult.geteMobile());
                        listStore.get(indexListStore).setePhoneHome(employeeResult.getePhoneHome());
                        break;
                    }
                }
            }
            showDetail(employeeResult);
            messageLabel.setText(" ");
        }
    }
}
